<script charset="utf-8" src="<?=STATIC_PATH?>/kindeditor/kindeditor-min.js"></script>
<script charset="utf-8" src="<?=STATIC_PATH?>/kindeditor/lang/zh_CN.js"></script>
<fieldset style="width:800px;border:solid 1px #A4BED4;margin-bottom:10px;margin-left:-1px;position:relative;">
    <legend>检索条件</legend>
    <div>
        <table style="width:100%;table-layout:fixed;">
            <tbody>
            <tr id="edit_boxs">
                <td style="width:100px;">机构：</td>
                <td style="width:150px;"><input id="agency" class="easyui-combotree" value="" data-options="url:'/oa/agencyListAll'" style="width:130px;"></td>

                <td style="width:40px;">员工：</td>
                <td style="width:150px;"><input id="staff" class="text"></td>
            </tr>
            <tr>
                <td>开始时间：</td>
                <td><input id="start_time" class="date" style="width:130px"></td>
                <td>截至时间</td>
                <td><input id="end_time" class="date" style="width:130px"></td>
                <td colspan="2" style="width: 100px;"><a href="javascript:void(0)" id="select" class="button"><span class="button-text">检索...</span></a></td>
            </tr>
            </tbody>
        </table>
    </div>
</fieldset>
<table id="weeklyReports"></table>
<fieldset style="width:1065px;border:solid 1px #A4BED4;margin-bottom:10px;margin-top:10px;margin-left:-1px;position:relative;">
    <legend>周报管理操作</legend>
    <form action="/oa/weeklyReportsAdd" method="post" enctype="multipart/form-data" id="weeklyReportsSubmit">
        <div>
            <input type="hidden" name="id" id="ClickRowID" value="">
            <table style="width:100%;table-layout:fixed;">
                <tbody>
                <tr>
                    <td style="width:100px;">选择日期：</td>
                    <td style="width:445px;">
                        <input name="dates" style="width: 250px;" id="dates" class="text date" required="true">
                    </td>
                </tr>
                <tr>
                    <td style="width:100px;">周报主题：</td>
                    <td style="width:445px;">
                        <input name="title" style="width: 250px;" class="text required" id="title" required="true">
                    </td>
                </tr>
                <tr>
                    <td style="width:100px;">上传附件：</td>
                    <td style="width:445px;">
                        <input type="file" name="attachment" id="attachment">
<!--                        <input name="attachment" style="width: 250px;" class="text required" id="attachment" required="true">-->
                    </td>
                </tr>
                <tr><td colspan="2">&nbsp;</td></tr>

                <tr>
                    <td style="width:100px;">周报内容：</td>
                    <td style="width:645px;">
                        <textarea name="content" id="ClickRowRemark" class="required"  style="width:642px;height:180px;"></textarea>
                    </td>
                </tr>
                <tr>
                    <td style="width: 100px;">&nbsp;</td>
                    <td style="width: 645px;">
                        <a href="javascript:void(0)" class="button" id="SaveButton">
                            <span class="button-text">提交</span></a>&nbsp;&nbsp;
                        <a href="javascript:void(0)" class="button" id="ResetButton">
                            <span class="button-text">重置</span>
                        </a>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
    </form>
</fieldset>

<script type="text/javascript">
    KindEditor.ready(function(K) {
        editor1 = K.create('#ClickRowRemark', {
            resizeType : 1,
            uploadJson : '/editorupload/upload',
            items : [
                'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'table','italic', 'underline',
                'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
                'insertunorderedlist', '|', 'emoticons', 'image', 'link']
        });
    });

    $(document).ready(function(){
        <?php if($suc) {?>
        parent.$.dooDialog.alert('编辑成功！');
        <?php }?>

        // 初期化DataGrid
        $('#weeklyReports').datagrid({
            width:1080,
            height:250,
            nowrap: true,
            autoRowHeight: false,
            striped: true,
            url:'/oa/weeklyReportsPattern',
            sortName: 'id',
            remoteSort: false,
            queryParams:{},
            idField:'id',
            pagination:true,
            rownumbers:true,
            pageList:[10,15,20],
            frozenColumns:[[
                {field:'ck',checkbox:true},
            ]],
            columns:[[
                {field:'id',title:'周报编号',width:80},
                {field:'title',title:'周报主题',width:285},
                {field:'staff_name',title:'员工',width:120},
                {field:'agency_name',title:'所属机构',width:180},
                {field:'position_name',title:'职位',width:120},
                {field:'dates',title:'日期',width:100,rowspan:2,sortable:true,
                    sorter:function(a,b){
                        return (a>b?1:-1);
                    }
                },
                {field:'created',title:'创建时间',width:125,rowspan:2}
            ]],
            toolbar:
                    [{
                        id:'btnadd',
                        text:'新增周报',
                        iconCls:'icon-add',
                        handler:function(){
                            var rows = $('#weeklyReports').datagrid('getSelections');
                            if( rows.length == 1 ){
                                var rowData = rows[0];
                                $('#ClickRowID').val();
                                $('#title').val(rowData.title);
                                $(".datebox input[type='text']").val(rowData.dates);
                                $("input[name='dates']").val(rowData.dates);
                                editor1.html(rowData.contents);
                                $('#SaveButton span').text('新增');
                            }else{
                                parent.$.dooDialog.alert('您只能复制一条记录，请重新选择......');
                            }
                        }
                    },'-',{
                        text:'删除选中周报',
                        iconCls:'icon-remove',
                        handler:function(){
                            var ids  = "";

                            parent.$.dooDialog.confirm("你确定要删除这个周报吗？",function(){
                                var rows = $('#weeklyReports').datagrid('getSelections');
                                if( rows.length > 0 ){
                                    for(var i=0;i<rows.length;i++){
                                        if(i==0){
                                            ids = rows[i].id;
                                        }else{
                                            ids = ids + "," + rows[i].id;
                                        }
                                    }

                                    $.post('/oa/weeklyReportsDel',{deleted:ids},function(data){
                                        if(data.err == 0){
                                            $("#weeklyReports").datagrid('reload');
                                        }else{
                                            parent.$.dooDialog.alert('周报删除失败！');
                                        }
                                    },'json');
                                }else{
                                    parent.$.dooDialog.alert('您还没有选择要删除的记录，请选择......');
                                }
                            });
                        }
                    },'-',{
                        text:'编辑选中周报',
                        iconCls:'icon-edit',
                        handler:function(){
                            var rows = $('#weeklyReports').datagrid('getSelections');
                            if( rows.length == 1 ){
                                var rowData = rows[0];
                                $('#ClickRowID').val(rowData.id);
                                $('#title').val(rowData.title);
                                $(".datebox input[type='text']").val(rowData.dates);
                                $("input[name='dates']").val(rowData.dates);
                                editor1.html(rowData.contents);
                                $('#SaveButton span').text('保存');
                            }else{
                                parent.$.dooDialog.alert('您只能编辑一条记录，请重新选择......');
                            }
                        }
                    },'-',{
                        text:'查看周报',
                        iconCls:'icon-search',
                        handler:function(){
                            var rows = $('#weeklyReports').datagrid('getSelections');
                            if( rows.length == 1 ){
                                var rowData = rows[0];
                                window.location.href = '/oa/weeklyReportsView/?id='+(rowData.id);
                                $('#SaveButton span').text('保存');
                            }else{
                                parent.$.dooDialog.alert('您只能查看一条记录，请重新选择......');
                            }
                        }
                    }],
            onUnselect:function(rowIndex, field, value){
                $('#ClickRowID').val();
                $('#ClickRowName').val('');
                $('#ClickRowTypes').val('');
                $('#ClickRowRemark').val('');
            }
        });

        // 下拉框设置
        $('#staff').combogrid({
            panelWidth:350,
            panelHeight:250,
            rownumbers:true,
            idField:'id',
            textField:'name',
            url:'/oa/staffPattern/',
            pagination:true,
            pageList:[10],
            columns:[[
                {field:'id',title:'ID',width:150},
                {field:'name',title:'姓名',width:154}
            ]]
        });

        // 检索功能
        $('a#select').click(function(){
            var queryParams = $('#weeklyReports').datagrid('options').queryParams;
            queryParams.agency = $('#agency').next("span").find("input[type='hidden']").val();
            queryParams.staff = $('#staff').next("span").find("input[type='hidden']").val();
            queryParams.start_time = $('#start_time').next("span").find("input[type='hidden']").val();
            queryParams.end_time = $('#end_time').next("span").find("input[type='hidden']").val();

            $('#weeklyReports').datagrid('options').queryParams=queryParams;
            $("#weeklyReports").datagrid('reload');
        });

        // 变更提交
        $('a#SaveButton').click(function(){
            // 数据检测
            var isValid = false;
            editor1.sync('ClickRowRemark');
            if ($("#ClickRowRemark").val() == '' || $("#title").val() == '' || $("input[name='dates']").val() == '') {
                isValid = true;
                return false;
            }

            // 检测结果
            if(isValid){
                return false
            }

            $('form#weeklyReportsSubmit').submit();
            return false;
            // 数据提交
            $.post($('form#weeklyReportsSubmit').attr('action'),$('form#weeklyReportsSubmit').serializeArray(),function(data){
                if (data.err == 0){
                    $("#weeklyReports").datagrid('reload');
                    $('input.text,input.number,input.count').each(function(){
                        $(this).val('');
                    });
                    $("#title").val('');
                    $('#ClickRowRemark').val('');
                    parent.$.dooDialog.alert('周报记录成功！');
                    return true;
                }else{
                    parent.$.dooDialog.alert(data.err);
                    return false;
                }
            },'json');
        });

        // 重置
        $('a#ResetButton').click(function(){
            $('input.text,input.number,input.count').each(function(){
                $(this).val('');
            });

            $('input[name="title"]').val('');
            $('input[name="content"]').val('');
            $('#ClickRowRemark').val('');
        });

    });
</script>